package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import one.stand.model.NoticeModel;
import one.stand.model.ResultModel;
import one.stand.request.NoticeRequest;
import one.stand.response.NoticeResponse;
import one.stand.service.BaseService;
import one.stand.service.NoticeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
public class NoticeServiceImpl extends BaseService implements NoticeService {

    @Override
    public ResultModel<Map<String, Object>> selectNoticeList(NoticeRequest request) {
        log.info("查询企业公告列表：{}", JSON.toJSONString(request));
        NoticeModel model = new NoticeModel();
        if(request != null) {
            BeanUtils.copyProperties(request, model);
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<NoticeModel> list = noticeMapper.selectList(model);
        if(list == null || list.size() <= 0){
            return ResultModel.noData();
        }
        PageInfo<NoticeModel> pageInfo = new PageInfo(list);
        Map<String, Object> map = new HashMap<>(1);
        map.put("pageInfo", pageInfo);
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<NoticeResponse> getNoticeInfo(NoticeRequest request) {
        if(request == null || request.getNoticeId() == null){
            return ResultModel.checkParamFail();
        }
        NoticeModel model = noticeMapper.selectByPrimaryKey(request.getNoticeId());
        NoticeResponse response = new NoticeResponse();
        BeanUtils.copyProperties(model, response);
        return ResultModel.success(response);
    }

    @Override
    public ResultModel<Boolean> insertNotice(NoticeRequest request) {
        log.info("添加企业公告信息：{}", JSON.toJSONString(request));
        if(request == null || StringUtils.isBlank(request.getNoticeTitle()) || StringUtils.isBlank(request.getNoticeContent())){
            return ResultModel.checkParamFail();
        }
        if(StringUtils.isBlank(request.getEndTimes()) || StringUtils.isBlank(request.getStartTimes())){
            return ResultModel.fail("请输入起止时间~！");
        }
        try {
            NoticeModel model = new NoticeModel();
            model.setNoticeTitle(request.getNoticeTitle());
            model.setNoticeContent(request.getNoticeContent());
            model.setNoticeJumpType(request.getNoticeJumpType());
            model.setNoticeJumpUrl(request.getNoticeJumpUrl());
            model.setNoticeType(request.getNoticeType());
            model.setStartTime(formatter.parse(request.getStartTimes()));
            model.setEndTime(formatter.parse(request.getEndTimes()));
            if(request.getNoticeId() == null){
                model.setIsDel(0);
                model.setCreateTime(currentDate());
                noticeMapper.insertSelective(model);
                return ResultModel.success(true);
            }
            model.setNoticeId(request.getNoticeId());
            noticeMapper.updateByPrimaryKeySelective(model);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ResultModel.success(true);
    }

    @Override
    public ResultModel<Boolean> delNotice(NoticeRequest request) {
        if(request == null || request.getNoticeId() == null){
            return ResultModel.checkParamFail();
        }
        NoticeModel model = new NoticeModel();
        model.setIsDel(1);
        model.setNoticeId(request.getNoticeId());
        noticeMapper.updateByPrimaryKeySelective(model);
        return ResultModel.success(true);
    }
}
